package com.itboye.hutouben.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.TimeCount;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * 注册2
 */
public class RegisterTwoActivity extends BaseActivity implements Observer {
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题
    TextView res_two_phone;//手机号
    EditText res_code;
    TextView res_two_time;//计时器
    TextView res_two_ok;

    String phone;
    private TimeCount time;// 倒计时

    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_two_activity);
        add_shap_title_tv.setText(R.string.input_code);

        userPresenter=new UserPresenter(this);

        phone = getIntent().getStringExtra("phone");
        res_two_phone.setText(phone);
        time = new TimeCount(60000, 1000, res_two_time);// 构造CountDownTimer对象
        time.start();

    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close_icon:
                finish();
                break;
            case R.id.res_two_ok://验证验证码
                if(res_code.getText().toString().equals("")){
                    ByAlert.alert("请输入验证码");
                    return;
                }
                showProgressDialog("请稍后...", true);
                userPresenter.YanZhengverificationCode("+86",phone,res_code.getText().toString(),1+"");

                break;
            case R.id.res_two_time:
                userPresenter.sendVerificationCode("+86",phone,1+"");

                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity=handlerError(data);
        if(entity!=null){
            IsUtilUid.tianjia(entity);
            if(entity.getEventType()==UserPresenter.send_code_success){
                ByAlert.alert(entity.getData());
                time.start();
            }
            if(entity.getEventType()==UserPresenter.send_code_fail){
                ByAlert.alert(entity.getData());
            }
            if(entity.getEventType()==UserPresenter.yanzheng_code_success){
                ByAlert.alert(entity.getData());
                Intent intent=new Intent(this,RegisterThreeActivity.class);
                intent.putExtra("phone", phone);
                intent.putExtra("code", res_code.getText().toString());
                startActivity(intent);
                RegisterTwoActivity.this.finish();
            }
            if(entity.getEventType()==UserPresenter.yanzheng_code_fail){
                ByAlert.alert(entity.getData());
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
